package com.zhangshuo.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.mybatisplus.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean;
import com.zhangshuo.common.dataSource.DynamicDataSource;
import org.apache.ibatis.plugin.Interceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.ConfigurationPropertiesBinding;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;
import java.util.Map;
import java.sql.SQLException;
import java.util.HashMap;

/**
 * Created by Administrator on 2017/5/31 0031.
 */
@Configuration
@MapperScan(value = {"com.zhangshuo.test1.mapper", "com.zhangshuo.test2.mapper"})
public class DataSourceConfig {


    @ConfigurationProperties(prefix = "datasource1")
    @Bean(name = "datasource1")
    /**
     * 在方法上注解configurationProperties时，将会把属性注入到返回结果的bean中
     */
    public DruidDataSource dataSource1() throws SQLException {
        return new DruidDataSource();
    }




    @ConfigurationProperties(prefix = "datasource2")
    @Bean(name = "datasource2")
    /**
     * 在方法上注解configurationProperties时，将会把属性注入到返回结果的bean中
     */
    public DruidDataSource dataSource2() throws SQLException {
        return new DruidDataSource();
    }



    @Bean(name = "datasource")
    public DynamicDataSource dynamicDataSource(@Qualifier(value = "datasource1") DataSource dataSource1, @Qualifier(value = "datasource2") DataSource dataSource2){
        DynamicDataSource bean = new DynamicDataSource();
        Map<Object,Object> targetDataSources = new HashMap<>();
        targetDataSources.put("datasource1",dataSource1);
        targetDataSources.put("datasource2", dataSource2);
        bean.setTargetDataSources(targetDataSources);
        bean.setDefaultTargetDataSource(dataSource1);
        return bean;
    }

/*    @Bean(name = "sessionFactory1")
    @ConfigurationProperties(prefix = "mybatis-plus1")
    @ConfigurationPropertiesBinding()
    @Primary
    public MybatisSqlSessionFactoryBean sqlSessionFactory1(@Qualifier(value = "datasource") DataSource dataSource) {
        MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        return bean;
    }

    @Bean(name = "sessionFactory2")
    @ConfigurationProperties(prefix = "mybatis-plus2")
    @ConfigurationPropertiesBinding()
    public MybatisSqlSessionFactoryBean sqlSessionFactory2(@Qualifier(value = "datasource") DataSource dataSource) {
        MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        return bean;
    }*/


    /**
     * 分页插件
     * @return
     */
    @Bean
    public PaginationInterceptor paginationInterceptor(){
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        paginationInterceptor.setDialectType("mysql");
        return paginationInterceptor;
    }



    /**
     * 生成sqlSessionFactory，因为使用了动态数据源，所以只用使用一次即可
     * @param dataSource
     * @return
     */
    @Bean(name = "sessionFactory")
    @DependsOn(value = {"datasource","paginationInterceptor"})
    @ConfigurationProperties(prefix = "mybatis-plus")
    @ConfigurationPropertiesBinding()
    public MybatisSqlSessionFactoryBean ssqlSessionFactoryBean(@Qualifier(value = "datasource") DataSource dataSource){
        MybatisSqlSessionFactoryBean mybatisSqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
        mybatisSqlSessionFactoryBean.setDataSource(dataSource);
        mybatisSqlSessionFactoryBean.setPlugins(new Interceptor[]{paginationInterceptor()});
        return mybatisSqlSessionFactoryBean;
    }

}
